﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel.DomainServices.Client;
using System.ComponentModel;
using Scoreboard.Basement.Views;
using Scoreboard.Basement.ViewModels;


namespace Scoreboard.Basement.Controls
{
	public partial class DialogWindow : ChildWindow
	{
		private IList<OperationBase> _possiblyPendingOperations = new List<OperationBase>();

		public FrameworkElement InitialContent
		{
			get { return (FrameworkElement)GetValue(InitialContentProperty); }
			set { SetValue(InitialContentProperty, value); }
		}
		
		public static readonly DependencyProperty InitialContentProperty =
			DependencyProperty.Register("InitialContent", typeof(FrameworkElement), typeof(DialogWindow), new PropertyMetadata(null));
		
		public FrameworkElement AlternateContent
		{
			get { return (FrameworkElement)GetValue(AlternateContentProperty); }
			set { SetValue(AlternateContentProperty, value); }
		}
		
		public static readonly DependencyProperty AlternateContentProperty =
			DependencyProperty.Register("AlternateContent", typeof(FrameworkElement), typeof(DialogWindow), new PropertyMetadata(null));
			
		public DialogWindow(DialogBase dialog)
		{
			InitializeComponent();
			
			ViewModel = dialog.ViewModel;
			InitialContent = dialog;

			var alternateContent = dialog.AlternateContent;
			if (alternateContent != null)
			{
				if (alternateContent.DataContext == null)
				{
					alternateContent.DataContext = dialog.ViewModel;
				}
				AlternateContent = alternateContent;				
			}
			
			this.LayoutUpdated += this.GoToInitialState;			
		}

		/*/// <summary>
		/// Notifies the window that it can only close
		/// if <paramref name="operation"/> is finished or can be cancelled
		/// </summary>
		/// <param name="operation">The pending operation to monitor</param>
		public void AddPendingOperation(OperationBase operation)
		{
			this._possiblyPendingOperations.Add(operation);
		}*/

		public DialogViewModel ViewModel
		{
			get
			{
				return DataContext as DialogViewModel;
			}
			set
			{
				DataContext = value;
				value.AttachView(this);
			}
		}

		public virtual void NavigateToInitialPanel()
		{
			VisualStateManager.GoToState(this, "AtInitialPanel", true);
		}

		public virtual void NavigateToAlternatePanel()
		{
			VisualStateManager.GoToState(this, "AtAlternatePanel", true);
		}
		
		/// <summary>
		/// Initializes the <see cref="VisualStateManager"/> for this component
		/// by putting it into the "AtInitialPanel" state
		/// </summary>
		private void GoToInitialState(object sender, EventArgs eventArgs)
		{
			VisualStateManager.GoToState(this, "AtInitialPanel", false);
			this.LayoutUpdated -= this.GoToInitialState;
		}				

		/// <summary>
		/// Prevents the window from closing while there are operations in progress
		/// </summary>
		/*private void DialogWindow_Closing(object sender, CancelEventArgs eventArgs)
		{
			foreach (OperationBase operation in this._possiblyPendingOperations)
			{
				if (!operation.IsComplete)
				{
					if (operation.CanCancel)
					{
						operation.Cancel();
					}
					else
					{
						eventArgs.Cancel = true;
					}
				}
			}
		}	*/	
	}
}
